package com.adtech.adms.system.datadictionary.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.adtech.adms.system.datadictionary.DataType;
import com.adtech.adms.system.datadictionary.pojo.Available;
import com.adtech.core.dao.impl.HibernateDAOImpl;
import com.adtech.core.exception.SystemException;
import com.adtech.core.util.ReflectUtil;

/**
 * 数据字典DAO实现类
 * 为数据字典提供具体的数据库操作功能
 * @author Dormin 
 * @since 2011-6-13 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class DataDictionaryDaoImpl extends HibernateDAOImpl implements DataDictionaryDAO {

	public List<?> getListByDataType(DataType dataType) throws SystemException{
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Class clazz = ReflectUtil.loadClass(dataType.getClassName());
		
		Criteria criteria = session.createCriteria(clazz);
		
		if (Available.class.isAssignableFrom(clazz)){
			criteria.add(Restrictions.eq("available", true));
		}
		
		if(dataType.getSort()!=null){
			if("desc".equals(dataType.getOrder())){
				criteria.addOrder(Order.desc(dataType.getSort()));
			}
			else{
				criteria.addOrder(Order.asc(dataType.getSort()));
			}
		}
		
		return criteria.list();
	}

}
